#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const int col=1e6+5;
int t,n;
long long a[N],c[col],nxt[N];
long long ans;
void dfs(int x,int B,int L,long long sum){
    if(x>n){
        ans=max(ans,sum);
        return;
    }
    dfs(x+1,a[x],L,sum+(B==a[x])*a[x]);
    dfs(x+1,B,a[x],sum+(L==a[x])*a[x]);
}
/*void f(){
    memset(c,-1,sizeof(c));
    nxt[0]=-1;
    int B=0,L=0;
    for(int i=1;i<=n;i++){
        //cout<<a[i]<<" ";
        nxt[i]=c[a[i]];
        c[a[i]]=i;
    }
    //cout<<"\n";
    //for(int i=1;i<=n;i++)
    //    cout<<nxt[i]<<" ";
    //cout<<"\n";
    for(int i=n;i>0;i--){
        if(a[B]==a[i]){
            B=i;
            ans+=a[i];
        }
        else if(a[L]==a[i]){
            L=i;
            ans+=a[i];
        }
        else if(nxt[B]==-1)
            B=i;
        else if(nxt[L]==-1)
            L=i;
        else{
            if(nxt[i]==-1){
                if(a[B]<=a[L])
                    B=i;
                else
                    L=i;
            }
            else{
                if(a[i]+a[B]>a[L])
                    L=i;
                else if(a[i]+a[L]>a[B])
                    B=i;
                else{

                }
            }
        }
    }
}*/
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        ans=0;
        //f();
        dfs(1,-1,-1,0);
        cout<<ans<<"\n";
    }
    return 0;
}
